<?
require_once ("../includes/func.php");
	header('Content-Type: application/xhtml+xml; charset=UTF-8', true);
	echo '<?xml version="1.0" encoding="UTF-8"?>';

if (file_exists("last-id.txt"))
{
	$fp = fopen('last-id.txt', 'rb');
	$lastIdInFile = fread($fp, 1024); //latest id in generated sitemap
}

$getNews = new GetNews;
$lastIdInTable = $getNews -> getLastId("blog", "id"); //latest id in in table

if ($lastIdInFile == $lastIdInTable) //if we have't new topics
{
	$fo = fopen('sitemap.xml', 'rb');
	while (!feof($fo))
		echo fread($fo, 2048); //print sitemap
	fclose($fo);
}
else
{
	ob_start(); //start buffering
?>
<urlset
      xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
            http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">

	<url>
	  <loc><?php echo HOME; ?>/</loc>
	  <changefreq>daily</changefreq>
	  <priority>1.00</priority>
	</url>
<?php
$myPager = new Pager;

$select = $myPager -> getSitemapNewsUrls("blog", "url"); //print topics pages
if (mysql_num_rows($select) > 0)
{
    while ($data = mysql_fetch_array($select))
    {
	?>
	<url>
		<loc><?php echo HOME . MYDIR . $data['url'] . EXTENSION; ?></loc>
		<changefreq>daily</changefreq>
		<priority>0.9</priority>
	</url>
	<?php
    }
}

$select1 = $myPager -> getSitemapNewsUrls("tags", "tag"); //print tags
if (mysql_num_rows($select1) > 0)
{
    while ($data = mysql_fetch_array($select1))
    {
	?>
	<url>
		<loc><?php echo HOME . MYDIR ."tags/". $data['tag']; ?></loc>
		<changefreq>daily</changefreq>
		<priority>0.8</priority>
	</url>
	<?php
    }
}
?>
</urlset>
<?php
$buffer = ob_get_contents(); //create var with cache
ob_end_clean(); //end buffering

//save cache
$fp = fopen('./sitemap.xml', 'w'); 
fwrite($fp, $buffer); 
fclose($fp);
//save information about last topic id
$fp = fopen('./last-id.txt', 'w'); 
fwrite($fp, $lastIdInTable); 
fclose($fp);
echo $buffer; //print sitemap
}
?>